﻿using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;

using LightReader.UserService;

namespace LightReader
{
	public partial class SplashPage : UserControl
	{
		public SplashPage()
		{
            // Required to initialize variables
            InitializeComponent();

            // this makes sure the subscribe/register form is properly displayed
            //registerForm.Height = App.Current.Host.Content.ActualHeight;
            //registerForm.Width = App.Current.Host.Content.ActualWidth;

            setupEventHandlers();
        }

        #region Event handlers
        void registerForm_UserAdded(object sender, UserAddedEventArgs e)
        {
            NavigationHelper.Navigate(new MainPage(e.User));
        }

        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            errorLabel.Visibility = Visibility.Collapsed;
            // login
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            EndpointAddress address = new EndpointAddress("http://localhost:4234/LightReader_Web/UserService.svc");

            var proxy = new UserService.UserServiceClient(binding, address);
            proxy.LoginUserCompleted += new System.EventHandler<LoginUserCompletedEventArgs>(proxy_LoginUserCompleted);           
            proxy.LoginUserAsync(userNameTextbox.Text, SecurityHelper.ComputeHash(passwordTextbox.Text));
        }

        void proxy_LoginUserCompleted(object sender, LoginUserCompletedEventArgs e)
        {           
            if (e.Result != null)
            {                
                NavigationHelper.Navigate(new MainPage(e.Result));
            }
            else
            {
                errorLabel.Text = "Invalid username/password.";
                errorLabel.Visibility = Visibility.Visible;
            }
        }

        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationHelper.Navigate(new Register());
        }
        #endregion

        #region Helper methods
        private void setupEventHandlers()
        {
            //registerButton.Click += (s, e) =>
            //{
            //    registerPopup.IsOpen = true;
            //};

            //registerForm.cancelButton.Click += (s, e) =>
            //{
            //    registerPopup.IsOpen = false;
            //};

            //App.Current.Host.Content.Resized += (s, e) =>
            //{
            //    registerForm.Width = App.Current.Host.Content.ActualWidth;
            //    registerForm.Height = App.Current.Host.Content.ActualHeight;
            //};

            //registerForm.UserAdded += new EventHandler<UserAddedEventArgs>(registerForm_UserAdded);
            submitButton.Click += new RoutedEventHandler(submitButton_Click);
        }
        #endregion
    }
}